﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Jii : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack )
        {
            string od = ConfigurationManager.ConnectionStrings["ConStr"].ToString();

            SqlConnection ct = new SqlConnection(od);
            try
            {
                ct.Open();
                string sct = "select * from tb_Department";
                SqlCommand cmd = new SqlCommand(sct,ct);
                SqlDataReader RD = cmd.ExecuteReader();

                remove.DataSource = RD;
                remove.DataTextField = "Department";
                    
                remove.DataValueField = "ID";
                remove.DataBind();

                RD.Close();

                string Ins = "select * from tb_Department";
                SqlCommand Lit = new SqlCommand(Ins, ct);
                SqlDataReader Use = Lit.ExecuteReader();


                DropDownList1.DataSource = Use;
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "Department";
                DropDownList1.DataBind();

                Use.Close();
                ct.Close();
            }
            catch(Exception ex)
            {
                ct.Close();
            }
            
        }
    }
    protected void confirm_Click(object sender, EventArgs e)
    {
        // string Cfn = "";    //提示
        string[] a=new string[20];//提示
        int b = 0;
        Int16 c = 0;
        string osql = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
        SqlConnection scn = new SqlConnection(osql);
        try
        {
            scn.Open();

            for (int i = 0; i < remove.Items.Count; i++)
            {
                if (remove.Items[i].Selected == true)
                {
                    a[b] = remove.Items[i].Value;
                    //update tb_Department set IsDel =IsDel-1 where ID = (select ID from tb_Department where ID =(select FID from tb_Department where ID = 44));
                    string execution ="update tb_Department set IsDel =IsDel-1 where ID = (select ID from tb_Department where ID =(select FID from tb_Department where ID ="
                        +"'"+remove.Items[i].Value+"'"+")); "
                        +"delete from tb_Department where ID ='" + remove.Items[i].Value + "'"+"and IsDel <= 0";
                    SqlCommand cmdd = new SqlCommand(execution, scn);
                    cmdd.ExecuteNonQuery();
                    b++;
                  //  scn.Close();
                }
                
            }
            for (c = 0; c < b; c++)
            {
                // scn.Open();  
                //a = null;
                string Drop1 = "select * from tb_Department where ID =" + "'" + a[c] + "';";
                SqlCommand cmd1 = new SqlCommand(Drop1, scn);
                SqlDataAdapter read1 = new SqlDataAdapter(cmd1);
               // Datatab table1 = new DataSet();
                DataSet table1 = new DataSet();
                read1.Fill(table1);

                //if (table1.Tables[0].Rows[c][0] != null)
                //{
                //    Response.Write("<script>alert('有建立的部门');</script>");//提示
                //   // Array.Clear(a[]);
                //    a = null;
                //    break;
                //}
                try
                { 
                    if (table1.Tables[0].Rows[c][0] != null)
                    {
                        Response.Write("<script>alert('有子部门不允许删除!');</script>");
                    }
                }
                catch(Exception exss)
                {
                   // Response.Write("<script>alert('有建立的部门');</script>");//提示
                }
            }
            //string Drop1 = "select * from tb_Department where ID="+"'"+a[c]+"'";
            //SqlConnection Dropct = new SqlConnection(Drop); //无用语句
            //SqlCommand Lits1= new SqlCommand(Drop1, scn);
            //SqlDataAdapter DropAter1 = new SqlDataAdapter(Lits1);
            //DataTable DropTable1 = new DataTable();
            //DropAter1.Fill(DropTable1);
           // string sct = "select * from tb_Department";
           // SqlCommand cmd = new SqlCommand(sct, scn);
           // SqlDataReader RD = cmd.ExecuteReader();
            string Drop = "select * from tb_Department";
            //SqlConnection Dropct = new SqlConnection(Drop); //无用语句
            SqlCommand Lits = new SqlCommand(Drop, scn);
            SqlDataAdapter DropAter = new SqlDataAdapter(Lits);
            DataTable DropTable = new DataTable();
            DropAter.Fill(DropTable);
           
            remove.DataSource = DropTable;
            remove.DataTextField = "Department";
            remove.DataValueField = "ID";
            remove.DataBind();
            DropDownList1.DataSource = DropTable;
            DropDownList1.DataTextField = "Department";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();
           

            scn.Close();

        }
        catch (Exception exs)
        {
            scn.Close();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text.Length != 0)
        {
            
            string stl = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
            string FtID = DropDownList1.SelectedItem.Value;
            Int16 DD = 0;
            SqlConnection conn = new SqlConnection(stl);
            try
            {
                //Add.OnClientClick = "return confirm('shi')";
                
                conn.Open();
                string cmdtext = "insert into tb_Department values('" + TextBox1.Text + "','" + FtID + "','" + DD + "'); " + "update tb_Department set IsDel = IsDel + 1 where ID ="+"'"+ FtID+"';";
                SqlCommand cmd = new SqlCommand(cmdtext, conn);
                cmd.ExecuteNonQuery();
                // update tb_Department set IsDel = IsDel + 1 where ID = FtID
                string Ins = "select * from tb_Department";
                SqlCommand Lit = new SqlCommand(Ins, conn);


                SqlDataAdapter adp = new SqlDataAdapter(Lit);
                DataTable Use = new DataTable();
                adp.Fill(Use);
                DropDownList1.DataSource = Use;
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "Department";
                DropDownList1.DataBind();
                //if (Use == null)
                //    ;
                //  ;
                remove.DataSource = Use;
                remove.DataTextField = "Department";
                remove.DataValueField = "ID";
                remove.DataBind();

                conn.Close();
            }
            catch (Exception ex)
            {
                conn.Close();
            }
        }
        
      else  Response.Write("<script>alert('请输入要建立的部门');</script>");
        TextBox1.Text = "";
    }
    protected void modify_Click(object sender, EventArgs e)
    {
        try
        {
            Session["UW"] = remove.SelectedItem.Value;
            //Session.Add("UW",ff );
            Response.Redirect("Modify.aspx");
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('请选择要修改的部门！');</script>");
        }
    }
}